package im.dhc.gitdiff.util;

/**
 * @author songhaichao
 * @create 2020-06-09-14:54
 **/
public class PathUtil {

    private static String javaPrefixDir = "src/main/java";
    private static String resourcePrefixDir = "src/main/resources";
    private static String webPrefixDir = "src/main/webapp";
    private static String testJavaPrefixDir = "src/test/java";
    private static String testResourcePrefixDir = "src/test/resources";
    private static String moduleVersion = "0.1.1-SNAPSHOT";
    private static String isModule = "Y";

    /**
     * 移除模块目录前缀后的path
     * @param allPath
     * @return
     */
    public static String removeModulePrefix(String allPath){
        if(allPath == null) return null;
        return allPath.substring(allPath.indexOf("src/"));
    }

    /**
     * 取得模块名
     * @param allPath
     * @return
     */
    public static String getModule(String allPath){
        return allPath.substring(allPath.indexOf("/")+1,allPath.indexOf("src/")-1).replace("dhc-","");
    }

    /**
     * 是否为java文件
     * @param path
     * @return
     */
    public static boolean isJavaFile(String path){
        return path.endsWith(".java");
    }

    /**
     * 是否为jar文件
     * @param path
     * @return
     */
    public static boolean isJarFile(String path){
        return path.endsWith(".jar");
    }

    /**
     * java文件替换为.class
     * @param path
     * @return
     */
    public static String replaceClassPath(String path){
        return path.replace(javaPrefixDir,"WEB-INF/classes").replace(".java",".class");
    }

    /**
     * 资源文件前缀替换
     * @param path
     * @return
     */
    public static String replaceResourcePath(String path){
        return path.replace(resourcePrefixDir,"WEB-INF/classes");
    }

    /**
     * web文件前缀替换
     * @param path
     * @return
     */
    public static String replaceWebappPath(String path){
        return path.replace(webPrefixDir,"").substring(1);
    }

    /**
     * java文件替换jar
     * @param path
     * @return
     */
    public static String replaceJarPath(String path){
        String module = getModule(path);
        return "WEB-INF/lib/"+module+"-"+moduleVersion+".jar";
    }

    public static String replaceTestClassPath(String path){
        return path.replace(testJavaPrefixDir,"WEB-INF/classes").replace(".java",".class");
    }

    public static String replaceTestResourcePath(String path){
        return path.replace(testResourcePrefixDir,"").substring(1);
    }

    public static String getPath(String path){
        if(path == null) return null;
        String absolutePath = removeModulePrefix(path);
        if(absolutePath.startsWith(resourcePrefixDir)){
            return replaceResourcePath(absolutePath);
        }else if(absolutePath.startsWith(webPrefixDir)){
            return replaceWebappPath(absolutePath);
        }else if(absolutePath.startsWith(javaPrefixDir)){
            if("Y".equals(isModule)){
                return replaceJarPath(path);
            }else{
                return replaceClassPath(absolutePath);
            }
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(PathUtil.removeModulePrefix("modules/dhc-stock-service/src/main/java/im/dhc/stock/pagemodel/StockReportBatchAdd.java"));
        System.out.println(PathUtil.getModule("modules/dhc-stock-service/src/main/java/im/dhc/stock/pagemodel/StockReportBatchAdd.java"));
        System.out.println(PathUtil.replaceWebappPath("src/main/webapp/im/dhc/stock/pagemodel/StockReportBatchAdd.jsp"));



    }
}
